package aorey.net.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class WebHttp {

	// 包含cookie的请求
	public static WebRequest get(String url, CookieStore cookiestore) {
		WebRequest webRequest = new WebRequest();
		CloseableHttpClient httpClient = HttpClients.custom().setDefaultCookieStore(cookiestore).build();
		HttpGet httpGet = new HttpGet(url);
		String html = "";
		HttpResponse response;
		try {
			response = httpClient.execute(httpGet);
			HttpEntity entity = response.getEntity();
			html = EntityUtils.toString(entity, "utf-8");
			webRequest.html = html;
			webRequest.cookies = cookiestore;
		} catch (Exception e) {
			
			e.printStackTrace();
		} finally {
			httpGet.releaseConnection();
		}
		return webRequest;
	}

	
	// 不包含cookie的请求
	public static WebRequest get(String url) {

		WebRequest webRequest = new WebRequest();
		CookieStore cookieStore = new BasicCookieStore();
		CloseableHttpClient httpClient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
		HttpGet httpGet = new HttpGet(url);
		String html = "";
		HttpResponse response;
		try {
			response = httpClient.execute(httpGet);
			HttpEntity entity = response.getEntity();
			html = EntityUtils.toString(entity, "utf-8");
//			System.out.println(html);
			webRequest.html = html;
			webRequest.cookies = cookieStore;
		} catch (Exception e) {
			
			e.printStackTrace();
		} finally {
			httpGet.releaseConnection();
		}
		return webRequest;
	}
	
	
	public static WebRequest post(String url,Map<String,String> data)
	{
		WebRequest webRequest = new WebRequest();
		CookieStore cookieStore = new BasicCookieStore();
		CloseableHttpClient httpClient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
		HttpPost httpPost = new HttpPost(url);
		HttpResponse response;
		String html = "";
		List<NameValuePair> params=new ArrayList<NameValuePair>();
		if(data!=null){  
            for (Entry<String, String> entry : data.entrySet()) {  
                //添加参数
                params.add( new BasicNameValuePair(entry.getKey(),entry.getValue()) );  
            }         
        }
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
			httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
			httpPost.setHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)");
			response = httpClient.execute(httpPost);
			HttpEntity entity = response.getEntity();
			html = EntityUtils.toString(entity, "utf-8");
			webRequest.html = html;
			webRequest.cookies = cookieStore;
		} catch (Exception e) {
			
		}
		finally {
			httpPost.releaseConnection();
		}
		
		return webRequest;
	}
	
	public static WebRequest post(String url,Map<String,String> data,CookieStore cookieStore)
	{
		WebRequest webRequest = new WebRequest();
		CloseableHttpClient httpClient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
		HttpPost httpPost = new HttpPost(url);
		HttpResponse response;
		String html = "";
		List<NameValuePair> params=new ArrayList<NameValuePair>();
		if(data!=null){  
            for (Entry<String, String> entry : data.entrySet()) {  
                //添加参数
                params.add( new BasicNameValuePair(entry.getKey(),entry.getValue()) );  
            }         
        }
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
			httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
			httpPost.setHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)");
			response = httpClient.execute(httpPost);
			HttpEntity entity = response.getEntity();
			html = EntityUtils.toString(entity, "utf-8");
			webRequest.html = html;
			webRequest.cookies = cookieStore;
		} catch (Exception e) {
			
		}
		finally {
			httpPost.releaseConnection();
		}
		
		return webRequest;
	}
	
	
}
